package com.cskaoyan.service.wx.impl;

import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.po.MarketTopic;
import com.cskaoyan.bean.po.MarketTopicExample;
import com.cskaoyan.bean.vo.wx.WxTopicListVo;
import com.cskaoyan.mapper.MarketTopicMapper;
import com.cskaoyan.service.wx.WxTopicService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @BelongsProject: market-project
 * @BelongsPackage: com.cskaoyan.service.wx.impl
 * @CreateTime: 2022-11-22  14:58
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class WxTopicServiceImpl implements WxTopicService {
    @Autowired
    MarketTopicMapper marketTopicMapper;


    @Override
    public CommonData selectById(Integer id) {
        //专题返回直接返回1页4个
        MarketTopic marketTopic = marketTopicMapper.selectByPrimaryKey(id);

        PageHelper.startPage(1, 4);
        List<MarketTopic> marketTopicList;
        String[] goods = marketTopic.getGoods();

        MarketTopicExample example = new MarketTopicExample();
        example.createCriteria();
        /*
        if (goods.length == 0) {
            example.setOrderByClause("update_time desc");
            marketTopicList = marketTopicMapper.selectByExample(example);
        } else {
            //构造example，根据goods查询同样有这个商品的专题，存入list中
            criteria.andGoodsLike(goods[0]);
            marketTopicList = marketTopicMapper.selectByExample(example);
        }*/
        //todo 专栏下的随机推荐，以何种方式进行推荐。代码记与此处
        example.setOrderByClause("update_time desc");
        marketTopicList = marketTopicMapper.selectByExample(example);

        PageInfo<MarketTopic> marketTopicPageInfo = new PageInfo<>(marketTopicList);
        return CommonData.data(marketTopicPageInfo);
    }

    @Override
    public CommonData<WxTopicListVo> select2WxTopicListVo(Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<MarketTopic> marketTopics = marketTopicMapper.selectByExample(null);
        List<WxTopicListVo> wxTopicListVos = new ArrayList<>();

        for (MarketTopic marketTopic : marketTopics) {
            WxTopicListVo wxTopicListVo = new WxTopicListVo();
            wxTopicListVo.setId(marketTopic.getId());
            wxTopicListVo.setPicUrl(marketTopic.getPicUrl());
            wxTopicListVo.setPrice(marketTopic.getPrice());
            wxTopicListVo.setReadCount(marketTopic.getReadCount());
            wxTopicListVo.setTitle(marketTopic.getTitle());
            wxTopicListVo.setSubtitle(marketTopic.getSubtitle());
            wxTopicListVos.add(wxTopicListVo);
        }

//        int size = wxTopicListVos.size();
//        int pages = size / limit;
//        if (size % limit != 0) {
//            pages++;
//        }
//
//
//        CommonData<WxTopicListVo> commonData = new CommonData<>();
//        commonData.setPage(page);
//        commonData.setLimit(limit);
//        commonData.setList(wxTopicListVos);
//        commonData.setTotal(size);
//        commonData.setPages(pages);
        PageInfo<WxTopicListVo> marketTopicPageInfo = new PageInfo<>(wxTopicListVos);
        return CommonData.data(marketTopicPageInfo);
    }
}


